package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.data.miniAdmin.IGoodsClassifyData;
import cc.iotkit.data.miniAdmin.IGoodsData;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminGoodsClassifyVo;
import cc.iotkit.manager.dto.vo.miniAdmin.MiniAdminGoodsVo;
import cc.iotkit.manager.service.IGoodsClassifyService;
import cc.iotkit.manager.service.IGoodsService;
import cc.iotkit.model.miniAdmin.Goods;
import cc.iotkit.model.miniAdmin.GoodsClassify;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class GoodsClassifyServiceImpl implements IGoodsClassifyService {

    private final IGoodsClassifyData  goodsClassifyData;


    @Override
    public void save(MiniAdminGoodsClassifyVo  goodsClassifyVo) {
        GoodsClassify goods = new GoodsClassify();
        BeanUtils.copyProperties(goodsClassifyVo,goods);
        goodsClassifyData.saveGoodsClassify(goods);
    }

    @Override
    public void update(MiniAdminGoodsClassifyVo goodsClassifyVo) {
        GoodsClassify goods = new GoodsClassify();
        BeanUtils.copyProperties(goodsClassifyVo,goods);
        goodsClassifyData.updateGoodsClassify(goods);
    }

    @Override
    public Paging<MiniAdminGoodsClassifyVo> getPageList(PageRequest<MiniAdminGoodsClassifyVo> query) {
//        return goodsClassifyData.findByConditions(query.to(GoodsClassify.class))
//                .to(MiniAdminGoodsClassifyVo.class);


        Paging pageing = new Paging<>();

        pageing = goodsClassifyData.findByConditions(query.to(GoodsClassify.class));

        List<GoodsClassify> goodList =  pageing.getRows();
        List<MiniAdminGoodsClassifyVo> goodsVos = new ArrayList<>();

        for (GoodsClassify goods:goodList){
            MiniAdminGoodsClassifyVo goodsClassifyVo  = new MiniAdminGoodsClassifyVo();
            BeanUtils.copyProperties(goods,goodsClassifyVo);
            goodsVos.add(goodsClassifyVo);
        }
        pageing.setRows(goodsVos);
        return  pageing;
    }

    @Override
    public void deleteGoods(Long id) {
        goodsClassifyData.deleteGoodsClassify(id);
    }
}
